<template>
  <div v-if="visible" class="modal-overlay" @click="handleOverlayClick">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>登录已失效</h3>
      </div>
      <div class="modal-body">
        <p>您的登录状态已过期，请重新登录</p>
      </div>
      <div class="modal-footer">
        <button class="modal-btn" @click="handleConfirm">重新登录</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const visible = ref(false)

// 接收外部传入的事件处理函数
const emit = defineEmits(['confirm', 'hide'])

// 显示弹窗
const show = () => {
  visible.value = true
}

// 隐藏弹窗
const hide = () => {
  visible.value = false
  emit('hide')
}

// 处理确认按钮点击
const handleConfirm = () => {
  console.log('用户点击重新登录按钮')
  hide()
  emit('confirm')
}

// 处理遮罩层点击
const handleOverlayClick = () => {
  handleConfirm()
}

// 暴露方法给外部调用
defineExpose({
  show,
  hide
})
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease-out;
}

.modal-content {
  background: white;
  border-radius: 12px;
  padding: 0;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: slideIn 0.3s ease-out;
}

.modal-header {
  padding: 20px 24px 0;
  text-align: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.modal-body {
  padding: 16px 24px;
  text-align: center;
}

.modal-body p {
  margin: 8px 0;
  color: #666;
  line-height: 1.5;
}

.modal-footer {
  padding: 0 24px 20px;
  text-align: center;
}

.modal-btn {
  padding: 8px 24px;
  background: #6278db;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.modal-btn:hover {
  background: #5a6fd8;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>
